package com.tensin.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tensin.mall.product.entity.AttrEntity;
import com.tensin.mall.product.service.AttrService;
import com.tensin.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tensin.common.utils.PageUtils;
import com.tensin.common.utils.Query;

import com.tensin.mall.product.dao.AttrGroupDao;
import com.tensin.mall.product.entity.AttrGroupEntity;
import com.tensin.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //判断params里面有没有带key，即有没有带查询条件。
        String key = (String) params.get("key");

        //catelogId相等
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(key)){
            wrapper.and((obj) ->{
                obj.eq(AttrGroupEntity::getAttrGroupId,key).or().like(AttrGroupEntity::getAttrGroupName,key);
            });
//                wrapper.and(new Consumer<LambdaQueryWrapper<AttrGroupEntity>>() {
//                    @Override
//                    public void accept(LambdaQueryWrapper<AttrGroupEntity> attrGroupEntityLambdaQueryWrapper) {
//                        attrGroupEntityLambdaQueryWrapper.eq(AttrGroupEntity::getAttrGroupId,key).or().like(AttrGroupEntity::getAttrGroupName,key);
//                    }
//                });
        }


        //如果catelogId为0，说明没有第三级分类，即查询所有
        if (catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            //catelogId有值，则查询对饮的三级分类
            wrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);

            return new PageUtils(page);
        }

    }


    /**
     * 1、查出当前分类下的所有属性分组，2、查出每个属性分组的所有属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //查出当前分类下的所有属性Entity
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
        List<AttrGroupEntity> list = this.list(wrapper);

        //查出所有属性
        List<AttrGroupWithAttrsVo> collect = list.stream().map(group -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrsVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(group.getAttrGroupId());
            attrsVo.setAttrs(relationAttr);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

}